package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/admin/dish")
@RestController
public class DishController {
    @Autowired
    private DishService dishService;
    /*新增菜品*/
    @PostMapping
    public Result save(@RequestBody DishDTO dishDTO){
        log.info("新增菜品：{}",dishDTO);
        dishService.save(dishDTO);
        return Result.success();
    }
    /*分页查询*/
    @GetMapping("/page")
    public Result<PageResult>page(DishPageQueryDTO dishPageQueryDTO){
        PageResult pageResult=dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);
    }
    /*根据id查询菜品*/
    @GetMapping("/{id}")
    public Result<DishVO> geById(@PathVariable Long id){
        log.info("根据id查询菜品：{}",id);
        DishVO dishVO=dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }
    /*根据分类Id查询菜品*/
    @GetMapping("/list")
    public Result<List<Dish>> list(Long categoryId){
        log.info("根据分类Id查询菜品：{}",categoryId);
        List<Dish> list=dishService.getDishByCategoryId(categoryId);
        return Result.success(list);
    }
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    /*菜品起售停售*/
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status,Long id){
        log.info("菜品起售停售：{},{}",status,id);
        dishService.startOrStop(status,id);
        return Result.success();
    }
    /*更新菜品*/
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("更新菜品：{}",dishDTO);
        dishService.update(dishDTO);
        return Result.success();
    }
    /*批量删除菜品*/
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    @DeleteMapping()
    public Result delete(@RequestParam List<Long> ids){
        log.info("批量删除菜品：{}",ids);
        dishService.delete(ids);
        return Result.success();
    }
}
